<template>
  <div class="login_wrapper">
    <div class="login_box">
      <!--头像-->
      <!-- <div class="head_content">
        <img src="../assets/logo.png" alt="">
      </div> -->
      <!--登录内容-->
      <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login_form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" prefix-icon="el-icon-lock" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts">
import { loginApi } from "@/request/api";
import { ElForm } from "element-ui/types/form";
import Vue from "vue";
import Component from "vue-class-component";
import { Ref } from "vue-property-decorator";
interface loginRulesType {
  required: boolean;
  message: string;
  trigger: string;
}
interface loginRulesType2 {
  min: number;
  max: number;
  message: string;
  trigger: string;
}
@Component
export default class Login extends Vue {
  loginForm: { username: string; password: string } = {
    username: "",
    password: "",
  };
  loginRules: {
    username: Array<loginRulesType | loginRulesType2>;
    password: Array<loginRulesType | loginRulesType2>;
  } = {
    username: [
      { required: true, message: "请输入用户名", trigger: "blur" },
      { min: 3, max: 6, message: "输入3到6个字符", trigger: "blur" },
    ],
    password: [
      { required: true, message: "请输入密码", trigger: "blur" },
      { min: 6, max: 12, message: "输入6到12个字符", trigger: "blur" },
    ],
  };

  @Ref("loginRef") loginRef!: ElForm;

  login() {
    this.loginRef.validate(async (valid: boolean) => {
      if (!valid) return;
      const { data, meta } = await loginApi(this.loginForm);
      console.log(data, meta);
      if (meta.status !== 200) return this.$message.error("fail");
      this.$message.success("success");
      // 保存token
      window.sessionStorage.setItem("token", data.token);
      // 跳转到主页面
      this.$router.push("/home");
    });
  }

  reset() {}
}
</script>

<style lang="less" scoped>
.login_wrapper {
  background-color: skyblue;
  height: 100%;
  .login_box {
    width: 450px;
    height: 260px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 3px;
    .head_content {
      width: 130px;
      height: 130px;
      border-radius: 50%;
      padding: 10px;
      box-sizing: border-box;
      position: absolute;
      background-color: #fff;
      transform: translate(-50%, -50%);
      left: 50%;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .login_form {
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
      position: absolute;
      bottom: 0px;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>